package cn.ld.module.infra.service.demo.demo01;

import cn.ld.module.infra.controller.admin.demo.demo01.vo.Demo01ContactPageInVO;
import cn.ld.module.infra.controller.admin.demo.demo01.vo.Demo01ContactSaveInVO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.ld.module.infra.dal.entity.demo.demo01.Demo01ContactEntity;
import cn.ld.framework.common.pojo.PageResult;
import cn.ld.framework.common.util.object.BeanUtils;

import cn.ld.module.infra.dal.mapper.demo.demo01.Demo01ContactMapper;

import static cn.ld.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.ld.module.infra.enums.ErrorCodeConstants.*;

/**
 * 示例联系人 Service 实现类
 *
 * @author 蓝盾
 */
@Service
@Validated
public class Demo01ContactServiceImpl implements Demo01ContactService {

    @Resource
    private Demo01ContactMapper demo01ContactMapper;

    @Override
    public Long createDemo01Contact(Demo01ContactSaveInVO createInVO) {
        // 插入
        Demo01ContactEntity demo01Contact = BeanUtils.toBean(createInVO, Demo01ContactEntity.class);
        demo01ContactMapper.insert(demo01Contact);
        // 返回
        return demo01Contact.getId();
    }

    @Override
    public void updateDemo01Contact(Demo01ContactSaveInVO updateInVO) {
        // 校验存在
        validateDemo01ContactExists(updateInVO.getId());
        // 更新
        Demo01ContactEntity updateObj = BeanUtils.toBean(updateInVO, Demo01ContactEntity.class);
        demo01ContactMapper.updateById(updateObj);
    }

    @Override
    public void deleteDemo01Contact(Long id) {
        // 校验存在
        validateDemo01ContactExists(id);
        // 删除
        demo01ContactMapper.deleteById(id);
    }

    private void validateDemo01ContactExists(Long id) {
        if (demo01ContactMapper.selectById(id) == null) {
            throw exception(DEMO01_CONTACT_NOT_EXISTS);
        }
    }

    @Override
    public Demo01ContactEntity getDemo01Contact(Long id) {
        return demo01ContactMapper.selectById(id);
    }

    @Override
    public PageResult<Demo01ContactEntity> getDemo01ContactPage(Demo01ContactPageInVO pageInVO) {
        return demo01ContactMapper.selectPage(pageInVO);
    }

}